package com.carryGo.service.lt;

import com.carryGo.dao.lt.BaseDao;
import com.carryGo.dao.lt.CommodityDao;
import com.carryGo.entity.Commodity;
import com.carryGo.entity.PageManager;
import com.carryGo.entity.User;

import java.io.File;
import java.util.Arrays;
import java.util.List;

/**
 * ClassName:UserService
 * package:
 * Desription:
 *
 * @Date:2020/8/12 19:30
 * @Author: tank_李涛
 *//*
 *FileName:  UserService
 * Author:   28633
 * Date  :   2020/8/12 19:30
 * */
public class CommodityService extends CommodityDao {


    public boolean addCommodity(Commodity commodity) {
        String sql = "INSERT INTO `rz1217db_4`.`commodity`(`commName`, `price`, `type`,`commInform`,`pic`,`businessName`) " +
                "VALUES (?, ?, ?,?,?,?)";
        Object[]params = getObjArr(commodity);
//        System.out.println(Arrays.toString(params));

        int update = update(sql, params);
        System.out.println(update);
        return update > 0;
    }


    private Object[] getObjArr(Commodity commodity) {
        Object[]params =
                {
                        commodity.getCommName(), commodity.getPrice(), commodity.getType(), commodity.getCommInform(),
                        commodity.getPic(), commodity.getBusinessName()
                };
        return params;
    }

    //查询所有数据
    public List<Commodity>queryCommodity(PageManager pm){
        int start = (pm.getPn() -1) * pm.getRn();
        int end = pm.getRn();
        String sql = "select * from commodity limit ?,?";
        return findList(Commodity.class,sql,start,end);
    }

    public boolean deleCommodity(int id) {
        String sql = "delete from  commodity where id = ?";
        Commodity commodity = queryCommodity(id);
        boolean result = update(sql, id) > 0;
        //如果结果为true则把本地的图片也要删除
        if (result){
            new File(commodity.getPic()).delete();
        }
        return result;
    }

    /**
     * 查询单条数据
     * @param id
     * @return
     */
    public Commodity queryCommodity(int id) {
        String sql = "select * from commodity where id = ?";
        return findBean(Commodity.class,sql,id);
    }

    public boolean updateCommodity(Commodity commodity) {
        String sql = "UPDATE `rz1217db_4`.`commodity` SET `commName` = ?, `price` = ?,`type` = ?, `commInform` , `pic`, `businessName`= ? WHERE `id` = ?";
        Object[] params = getObjArr(commodity);



       //查询单条数据
        Commodity queryCommodity = queryCommodity(commodity.getId());

        int update = update(sql, params);
        System.out.println(update);
        try {
            boolean result = update(commodity) > 0;
            System.out.println(result);
            //更换头像成功之后将原头像删除
            if (result && queryCommodity.getPic() != null && !queryCommodity.getPic().equals(commodity.getPic())) {
                //删除原图片
                new File(queryCommodity.getPic()).delete();
            }
            return result;
        }catch (Exception e){
            e.printStackTrace();
        }
        return update > 0?true:false;
    }

    public int queryCommodityTotalCount(){
        String sql = "select count(*) from commodity";
        return count(sql);
    }

    public Commodity queryCommodityByName(String commodity){
        String sql = "select * from commodity where commName = ?";
        return findBean(Commodity.class,sql,commodity);
    }


    /**
     * 查询所有数据
     */
    public List<Commodity> queryCommoditys() {
        String sql = "select * from commodity";
        return findList(Commodity.class,sql);
    }
}

